using System.Web.Mvc;
using GoldenBicycleCompany.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using System;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class BicycleRentalRatesController : Controller
    {
        public BicycleRentalRatesController(IRepository<BicycleRentalRate> bicycleRentalRateRepository) {
            Check.Require(bicycleRentalRateRepository != null, "bicycleRentalRateRepository may not be null");

            this.bicycleRentalRateRepository = bicycleRentalRateRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<BicycleRentalRate> bicycleRentalRates = bicycleRentalRateRepository.GetAll();
            return View(bicycleRentalRates);
        }

        [Transaction]
        public ActionResult Show(int id) {
            BicycleRentalRate bicycleRentalRate = bicycleRentalRateRepository.Get(id);
            return View(bicycleRentalRate);
        }

        public ActionResult Create() {
            BicycleRentalRateFormViewModel viewModel = BicycleRentalRateFormViewModel.CreateBicycleRentalRateFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(BicycleRentalRate bicycleRentalRate) {
            if (ViewData.ModelState.IsValid && bicycleRentalRate.IsValid()) {
                bicycleRentalRateRepository.SaveOrUpdate(bicycleRentalRate);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The bicycleRentalRate was successfully created.";
                return RedirectToAction("Index");
            }

            BicycleRentalRateFormViewModel viewModel = BicycleRentalRateFormViewModel.CreateBicycleRentalRateFormViewModel();
            viewModel.BicycleRentalRate = bicycleRentalRate;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            BicycleRentalRateFormViewModel viewModel = BicycleRentalRateFormViewModel.CreateBicycleRentalRateFormViewModel();
            viewModel.BicycleRentalRate = bicycleRentalRateRepository.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(BicycleRentalRate bicycleRentalRate) {
            BicycleRentalRate bicycleRentalRateToUpdate = bicycleRentalRateRepository.Get(bicycleRentalRate.Id);
            TransferFormValuesTo(bicycleRentalRateToUpdate, bicycleRentalRate);

            if (ViewData.ModelState.IsValid && bicycleRentalRate.IsValid()) {
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The bicycleRentalRate was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                bicycleRentalRateRepository.DbContext.RollbackTransaction();

				BicycleRentalRateFormViewModel viewModel = BicycleRentalRateFormViewModel.CreateBicycleRentalRateFormViewModel();
				viewModel.BicycleRentalRate = bicycleRentalRate;
				return View(viewModel);
            }
        }

        private void TransferFormValuesTo(BicycleRentalRate bicycleRentalRateToUpdate, BicycleRentalRate bicycleRentalRateFromForm) {
			bicycleRentalRateToUpdate.BicycleType = bicycleRentalRateFromForm.BicycleType;
			bicycleRentalRateToUpdate.SpecialEvent = bicycleRentalRateFromForm.SpecialEvent;
			bicycleRentalRateToUpdate.Season = bicycleRentalRateFromForm.Season;
			bicycleRentalRateToUpdate.Rate = bicycleRentalRateFromForm.Rate;
			bicycleRentalRateToUpdate.IsSeasonalRate = bicycleRentalRateFromForm.IsSeasonalRate;
			bicycleRentalRateToUpdate.IsSpecialEventRate = bicycleRentalRateFromForm.IsSpecialEventRate;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The bicycleRentalRate was successfully deleted.";
            BicycleRentalRate bicycleRentalRateToDelete = bicycleRentalRateRepository.Get(id);

            if (bicycleRentalRateToDelete != null) {
                bicycleRentalRateRepository.Delete(bicycleRentalRateToDelete);

                try {
                    bicycleRentalRateRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the bicycleRentalRate from being deleted. " +
						"Another item likely depends on this bicycleRentalRate.";
                    bicycleRentalRateRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The bicycleRentalRate could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

		/// <summary>
		/// Holds data to be passed to the BicycleRentalRate form for creates and edits
		/// </summary>
        public class BicycleRentalRateFormViewModel
        {
            private BicycleRentalRateFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static BicycleRentalRateFormViewModel CreateBicycleRentalRateFormViewModel() {
                BicycleRentalRateFormViewModel viewModel = new BicycleRentalRateFormViewModel();
                
                return viewModel;
            }

            public BicycleRentalRate BicycleRentalRate { get; internal set; }
        }

        private readonly IRepository<BicycleRentalRate> bicycleRentalRateRepository;
    }
}
